package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.net.Entity.ChannelBillData;
import com.iptv.grape.net.NetworkService;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.util.ReadJsonFile;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

// 节目单的 集合类
public class ChannelBillList {

    private List<ChannelBillContainer> billDatas = new ArrayList<ChannelBillContainer>();
    //private List<ChannelBill> bills = new ArrayList<ChannelBill>();

    //public int size() {
        //return this.bills.size();
    //}

    //public List<ChannelBill> getBills() {
        //return bills;
    //}


    public boolean syncData() {
        try {
            List<String> fileNames = CommUtil.getDateString();
            List<String> allFileList = ReadJsonFile.getPathList("channelBillData_");
            for (int i = 0; i < allFileList.size(); i++) {
                String tmpFileName = allFileList.get(i);
                String dateString = CommUtil.getFileNameStringDate(tmpFileName);
                if (!CommUtil.findDateStringHelp(fileNames, dateString)) {
                    // 如果不存在 则删除该文件 始终值保持7天的节目单
                    ReadJsonFile.deleteFile(tmpFileName);
                    AppLog.getCurLog().out("删除文件" + tmpFileName);
                }
            }
            List<ChannelBillContainer> tmpBillDatas = new ArrayList<ChannelBillContainer>();
            for (int i = 0; i < fileNames.size(); i++) {
                String fileName = "channelBillData_" + fileNames.get(i) + ".json";
                String dateString = fileNames.get(i);
                //AppLog.getCurLog().out(fileName);
                List<ChannelBillData> resultList = NetworkService.getInstance().getChannelListDetailsByDateAsync(dateString);
                if (resultList != null) {
                    serializeToFile(resultList, dateString);
                    initDataSyncData(resultList, dateString,tmpBillDatas);
                }
            }
            this.billDatas.clear();
            this.billDatas = tmpBillDatas;
            return true;
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    private void initDataSyncData(List<ChannelBillData> resultList, String dateString,List<ChannelBillContainer> tmpBillDatas){

        for (ChannelBillData billData : resultList) {
            ChannelBill bill = new ChannelBill(billData);
            ChannelBillContainer item = isExists(tmpBillDatas,billData.getChannelID(),dateString);
            if(item == null){
                item = new ChannelBillContainer(billData.getChannelID(),dateString);
                tmpBillDatas.add(item);
            }
            //item.getBills().clear();
            item.getBills().add(bill);
        }
        for(ChannelBillContainer item:tmpBillDatas){
            item.sortList();
        }
    }

    // 根据ID查找对应的节目单
    public ChannelBill findChannelBillByID(String keyID){
        for(int i = 0; i < billDatas.size(); i++){
            ChannelBillContainer billContainer = billDatas.get(i);
            for(int j = 0; j < billContainer.getBills().size();j++){
                ChannelBill channelBill = billContainer.getBills().get(j);
                if(channelBill.getKeyID().equals(keyID)){
                    return channelBill;
                }
            }
        }
        return null;
    }

    public static void serializeToFile(List<ChannelBillData> resultList,String dateString){
        try {
            Gson gson = new Gson();
            String json = gson.toJson(resultList);
            String fileName = "channelBillData_" + dateString + ".json";
            ReadJsonFile.writeFile(fileName, json);
            AppLog.getCurLog().out("保存节目单到文件" + fileName);
        }
        catch (Exception e){

        }
    }

    public static List<ChannelBillData> unSerializeToFile(String fileName){
        List<ChannelBillData> resultList = null;
        try {
            String json = ReadJsonFile.readFile(fileName);
            Type type = new TypeToken<List<ChannelBillData>>() {}.getType();
            resultList = new Gson().fromJson(json, type);
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return resultList;

    }


    // 初始化领域模型对象
    // 检查并获得前几天的节目单数据
    public void init() {
        List<String> fileNames = CommUtil.getDateString();
        List<String> allFileList = ReadJsonFile.getPathList("channelBillData_");
        for(int i = 0; i < allFileList.size(); i++){
            String tmpFileName = allFileList.get(i);
            String dateString = CommUtil.getFileNameStringDate(tmpFileName);
            if(!CommUtil.findDateStringHelp(fileNames,dateString)){
                // 如果不存在 则删除该文件 始终值保持7天的节目单
                ReadJsonFile.deleteFile(tmpFileName);
                AppLog.getCurLog().out("删除文件" + tmpFileName);
            }
        }
        for(int i = 0; i < fileNames.size(); i++){
            String fileName = "channelBillData_" + fileNames.get(i) + ".json";
            String dateString = fileNames.get(i);
            //AppLog.getCurLog().out(fileName);
            List<ChannelBillData> resultList = null;
            //if(i == 0 || !ReadJsonFile.fileExists(fileName)){
            if(!ReadJsonFile.fileExists(fileName)){
                // 访问网络 保存文件
                //AppLog.getCurLog().out(fileName + "不存在");
                resultList = NetworkService.getInstance().getChannelListDetailsByDateAsync(dateString);
                if(resultList != null) {
                    serializeToFile(resultList, dateString);
                    initData(resultList, dateString);
                }
            }
            else {
                // 从文件中 反序列化
                resultList = unSerializeToFile(fileName);
                if(resultList != null) {
                    initData(resultList, dateString);
                }
            }
        }
    }

    private void initData(List<ChannelBillData> resultList, String dateString){


        for (ChannelBillData billData : resultList) {
            ChannelBill bill = new ChannelBill(billData);
            ChannelBillContainer item = isExists(billData.getChannelID(),dateString);
            if(item == null){
                item = new ChannelBillContainer(billData.getChannelID(),dateString);
                billDatas.add(item);
            }
            //item.getBills().clear();
            item.getBills().add(bill);
        }
        for(ChannelBillContainer item:billDatas){
            item.sortList();
        }
    }



    // tmpBillDatas中查找是否存在ChannelBillContainer对象
    public ChannelBillContainer isExists(List<ChannelBillContainer> tmpBillDatas,String channelID,String dateString){
        for(ChannelBillContainer item:tmpBillDatas){
            if(item.getChannelID().equals(channelID) &&
                    item.getDateTimeString().equals(dateString)){
                return item;
            }
        }
        return null;
    }

    // billDatas中查找是否存在ChannelBillContainer对象
    public ChannelBillContainer isExists(String channelID,String dateString){
        for(ChannelBillContainer item:billDatas){
            if(item.getChannelID().equals(channelID) &&
                    item.getDateTimeString().equals(dateString)){
                return item;
            }
        }
        return null;
    }

    // 根据频道ID来获得对应的节目单
    public List<ChannelBill> getBillsByChannelID(String channelID) {
        String dateString = CommUtil.changeTime(new Date());
        return getBillsByChannelID(channelID,dateString);
    }

    public List<ChannelBill> getBillsByChannelID(String channelID, String dateString) {
        ChannelBillContainer item = isExists(channelID,dateString);
        if(item == null){
            return null;
//            item = new ChannelBillContainer(channelID,dateString);
//            List<ChannelBillData> resultList = NetworkService.getInstance().getChannelListDetails(channelID, dateString);
//            if (resultList != null) {
//                for (ChannelBillData billData : resultList) {
//                    ChannelBill bill = new ChannelBill(billData);
//                    item.getBills().add(bill);
//                }
//                item.sortList();
//            }
//            item.setAccessNetwork(true);
//            billDatas.add(item);
        }
        return item.getBills();
    }
}
